/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Petani;

import Daerah.Daerah;
import Tools.Koneksi;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author gatz
 */
public class PetaniKontrol {
    private Connection conn;

    public PetaniKontrol(Connection koneksi) {
        this.conn = koneksi;
    }

    public static PetaniKontrol getKoneksiPetani() throws SQLException {
        PetaniKontrol kon = new PetaniKontrol(Koneksi.bukaKoneksi());
        return kon;
    }

    public String cariIdPetani(String kode) throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;
        long idpetani=0;
        String a;
        try {
            conn.setAutoCommit(false);
            String sql = "select max(idpetani) from petani where daerah like '" + kode + "'";
            statement = conn.prepareStatement(sql);
            result = statement.executeQuery();
            while (result.next()) {
                a= result.getString("max(idpetani)");
                if(a==null){
                    idpetani=0;
                    break;
                }
                String b = a.substring(5,8);
                idpetani=Long.parseLong(b);
            }
            conn.commit();
        } catch (SQLException ex) {
            Logger.getLogger(PetaniKontrol.class.getName()).log(Level.SEVERE, null, ex);
        }
        idpetani= idpetani + 1;
        String fix = String.valueOf(idpetani);
        return fix;
    }
    public void tambahPetani(Petani petani) throws SQLException {
        String id = petani.getIdPetani();
        String nama = petani.getNamaPetani();
        String daerah = petani.getDaerah().getIdDaerah();
        String sql = "insert into Petani (IdPetani, NamaPetani,Daerah)"
                + "values ('" + id + "','" + nama + "','" + daerah + "')";

        Statement stat = conn.createStatement();
        stat.executeUpdate(sql);
        conn.close();
    }
     
        public void updatePetani(Petani petani) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "update Petani set Daerah=?, NamaPetani=? where IdPetani=?";
            statement = conn.prepareStatement(sql);
            statement.setString(1, petani.getDaerah().getIdDaerah());
            statement.setString(2, petani.getNamaPetani());
            statement.setString(3, petani.getIdPetani());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        }
    }


    public void deletePetani(Petani petani) throws SQLException {
        String id = petani.getIdPetani();
        String sql = "delete Petani "
                + "where IdPetani='" + id + "'";
        Statement stat = conn.createStatement();
        stat.executeUpdate(sql);
        conn.close();
    }

    public List<Petani> lihatSeluruhPetani() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;

        conn.setAutoCommit(false);
        statement = conn.prepareStatement("select Petani.IdPetani, Petani.NamaPetani, "
                + "Daerah.iddaerah, Daerah.namadaerah from Petani,Daerah "
                + "where Petani.daerah=Daerah.iddaerah");
        result = statement.executeQuery();
        List<Petani> petani = new ArrayList<Petani>();

        while (result.next()) {
            Petani petani2 = new Petani();
            Daerah dr = new Daerah();
            petani2.setIdPetani(result.getString("IdPetani"));
            petani2.setNamaPetani(result.getString("NamaPetani"));
            dr.setIdDaerah(result.getString("iddaerah"));
            dr.setNamaDaerah(result.getString("namadaerah"));
            petani2.setDaerah(dr);
            petani.add(petani2);
        }
        conn.commit();
        return petani;
    }
}
